/*
 * InsurancePlan.java
 *
 * This program uses the if-else-if construct to calculate the
 * installment amount for an insurance plan.
 *
 
 */

package com.javacore.day3.insurance;

import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

/**
 * 
 * @author
 */

public class InsurancePlan {
	static boolean isInsure = false;
	static boolean isYear = false;
	static boolean isChoice = false;

	/** Creates a new instance of InsurancePlan */
	public InsurancePlan() {
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {
		// Declare and initialize the variables
		double installment = 0;
		int amount = 0, choice = 0, years = 0;
		Scanner input = new Scanner(System.in);

		// Accepting the data for the insurance plan
		do {

			System.out.println("Enter the amount you want to insure for: $");
			String stramount = input.nextLine();
			try {
				amount = Integer.parseInt(stramount);
				isInsure = true;
			} catch (Exception ex) {
				isInsure = false;
				System.out.println("Insure invalid!Please try again!");
			}
		} while (!isInsure);
		do {
			System.out
					.println("Enter the number of years you want to insure for:");
			try {
				String strYear = input.nextLine();
				years = Integer.parseInt(strYear);
				isYear = true;
			} catch (Exception ex) {
				isYear = false;
				System.out.println("Number of year");
			}
		} while (!isYear);
		do {
			System.out
					.println("The following plans are available. Enter your choice:\n"
							+ " 1. Monthly \n 2. Half-Yearly \n 3. Yearly");
			try {
				String strChoice = input.nextLine();
				choice = Integer.parseInt(strChoice);
				if (choice > 3) {
					isChoice = false;
				} else {
					isChoice = true;
				}
			} catch (Exception ex) {
				System.out.println("Choice invalid!Please try again!");
				isChoice = false;
			}
		} while (!isChoice);
		// Calculate the premium depending on the choice of the user
		if (isChoice == true) {
			if (choice == 1) {
				installment = (amount / (years - 2)) / 12;
			} else if (choice == 2) {
				installment = (amount / (years - 2)) / 2;
			} else if (choice == 3) {
				installment = (amount / (years - 2));
			} else {
				System.out
						.println("Invalid Choice. The valid choices are 1, 2 or 3.");
				System.exit(0);
			}

			// Displaying the policy details
			System.out.println("\nPolicy Details:");
			System.out.printf("Insurance Amount: \t\t $%d \n", amount);
			System.out.printf("Insurance duration (years): \t %d  \n", years);
			if (choice == 1) {
				System.out.println("Insurance Plan: \t\t Monthly \n");
			} else if (choice == 2) {
				System.out.println("Insurance Plan: \t\t Half-Yearly \n");
			} else if (choice == 3) {
				System.out.println("Insurance Plan: \t\t Yearly \n");
			}
			System.out.printf("Installment : \t\t\t $%.2f \n", installment);
		}
	}
}
